﻿using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace AnlianCommon.API.Commands.Fields
{
    public class FieldCreateMoreHander: IRequestHandler<FieldCreateMoreCommand, Result>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Field> _fieldRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_cottageRepository"></param>
        /// <param name="mapper"></param>
        public FieldCreateMoreHander(IBaseRepository<Field> _fieldRepository, IBaseRepository<Cottage> _cottageRepository, IMapper mapper)
        {
            this._fieldRepository = _fieldRepository;
            this._mapper = mapper;
        }

        /// <summary>
        /// 批量添加栏位
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FieldCreateMoreCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Field field = _mapper.Map<Field>(request);
                field.CreatedOwnerId = "张三";
                field.CreatedTime = DateTime.Now;
                var list = await _fieldRepository.Add(field);
                if (list > 0)
                {
                    field.CreatedOwnerId = "张三";
                    field.CreatedTime = DateTime.Now;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
